<?php

!defined('IN_APP') && exit('Access Denied');

class controlbase {

	public  $ip;
	public  $time;
	public  $db;//mongodb
	public  $cache;
	public  $user = array();
	protected  $usergroup = array();
	public  $setting = array();

	protected  $get = array();
	protected  $post = array();
	protected  $regular; //当前访问的规则，例如 category/view
	
    function __construct( $get,  $post) {
        $this->time = time();
        $this->ip = getip();
        $this->get =  $get;
        $this->post = $post;
        $this->init_db();
        $this->init_cache();
        $this->init_user();
    }

	/* $eventlist=$this('event')->get_list(); 
	本特性只在PHP 5.3.0 及以上版本有效。 
	*/
	function __invoke($modelname, $base = NULL) {
		$base = $base ? $base : $this;
		if (empty($_ENV[$modelname])) {
		   require APP_ROOT . "/model/$modelname.class.php";
		   eval('$_ENV[$modelname] = new ' . $modelname . 'model($base);');
		}
		return $_ENV[$modelname];
	}

    function init_db() {
		$m= new Mongo('mongodb://'.DB_USER.':'.DB_PW.'@'.DB_HOST.':'.DB_PORT.'/'.DB_NAME);
		$this->db = $m->selectDB(DB_NAME);
        //$db->authenticate($username, $password);
        //$this->db =$m->mydata;//选择mydata数据库 ,这种写法也行。
    }
    


    /* 一旦setting的缓存文件读取失败，则更新所有cache */

    function init_cache() {
        global $setting;
        $this->cache = new cache($this->db);
        $setting = $this->setting = $this->cache->load('setting');
        /*
        foreach($setting as $k=>$v){
        	$this->db->selectCollection('setting')->insert(array('k'=>$k,'v'=>$v));
        }
        */
        $this->usergroup = $this->cache->load('usergroup', 'id');
    }



    function init_user() {
        @$sid = tcookie('sid');
        @$auth = tcookie('auth');
        $user = array();
        @list($uid, $password) = empty($auth) ? array(0, 0) : taddslashes(explode("\t", strcode($auth, $this->setting['auth_key'], 'DECODE')), 1);
        if (!$sid) {
            $sid = random(6);
            tcookie('sid', $sid, 31536000);
        }
        if ($uid && $password) {
            $uid=intval($uid);//这句很重要，不然mongodb不识别，找不出数据来
            $user = $this('user')->get_by_uid($uid);
            ($password != $user['password']) && $user = array();
        }
        if (!$user) {
            $user['uid'] = 0;
            $user['groupid'] = 0;
        } 
        $user['sid'] = $sid;
        $user['ip'] = $this->ip;
        $user['loginuser'] = $user['username'];
        $this->user =$user;
        $this->user = array_merge($user, $this->usergroup[$user['groupid']]);

    }

    /* 权限检测 */
    function checkable($url) {
        $this->regular = $url;
        if (1 == $this->user['groupid']) return true;
        $regulars = explode(',', 'user/login,user/logout,user/code,user/getpass,user/resetpass,index/help,js/view,' . $this->user['regulars']);
        return in_array($url, $regulars);
    }


    /*中转提示页面
      $ishtml=1 表示是跳转到静态网页
     */
    function message($message, $url = '') {
        $seotitle = '操作提示';
        if ('' == $url) {
            $redirect = SITE_URL;
        } else if ('BACK' == $url) {
            $redirect = $url;
        } else {
            $redirect = SITE_URL . $this->setting['seo_prefix'] . $url;
        }
        $tpldir = (0 === strpos($this->get['c'], 'admin')) ? 'admin' : $this->setting['tpl_dir'];
        include template('tip', $tpldir);
        exit;
    }

    /* 检查验证码 */
    function checkcode() {
        if (strtolower(trim($this->post['code'])) != $this('user')->get_code()) {
            $this->message("验证码错误!", 'BACK');
        }
    }

}


